#include <setjmp.h>
#include "../ourhdr.h"

static void f1(int,int,int);
static void f2(void);

static jmp_buf jmpbuffer;
/***
 * 指令未优化，值不回滚
zhaofeng@bogon 07 % gcc -g longjmp-var.c ../ourhdr.o -o longjmp-var.cmd
zhaofeng@bogon 07 % ./longjmp-var.cmd
in f1(): count=97, val=98, sum=99
after longjmp: count=97, val=98, sum=99
 * 指令优化，volatile也不会回滚
zhaofeng@bogon 07 % gcc -O longjmp-var.c ../ourhdr.o -o longjmp-var2.cmd
zhaofeng@bogon 07 % ./longjmp-var2.cmd                                  
in f1(): count=97, val=98, sum=99
 **/
int main(void)
{
    int count;
    register int val;
    volatile int sum;

    count=2;
    val = 3;
    sum = 4;

    if(setjmp(jmpbuffer) != 0)
    {
        printf("after longjmp: count=%d, val=%d, sum=%d\n"
            , count, val, sum);
        exit(0);
    }

    count=97;
    val = 98;
    sum = 99;

    f1(count, val, sum);
}

static void f1(int i, int j, int k)
{
    printf("in f1(): count=%d, val=%d, sum=%d\n"
        ,i,j,k);
    f2();
}

static void f2()
{
    longjmp(jmpbuffer, 1);
}
